<?php
class AudienceAction extends Action {
	
private $audience_id = "`audience_id`";
private $audience_list_cows="`audience_id`, `audience_name`, `audience_department`, `audience_position`, `audience_portrait`, `audience_imUsername` , `audience_imPassword`";

//用户登录
public function login() {
	//检测数据完整性
	switch(''){
		case $_REQUEST["username"]:return_json(40014);return;
		case $_REQUEST["password"]:return_json(40012);return;
	}
	//查询cid
	if(!Database($_REQUEST["cid"]))return;
	//判断当前company是否关闭
	if(!D('Admin/Company')->check_disabled($_REQUEST["cid"])){
		return_json(40010);
		return;
	}
	//判断当前用户名是否存在
	$audience = D('Audience')->get_audience('',$_REQUEST["username"]);
	if(!$audience){
		return_json(40015);
		return;
	}
	//判断当前用户名不可用
	if ($audience['audience_state'] == 0) {
		return_json(40019);
		return;
	}
	//判断密码是否正确
	if ($audience['audience_pwd'] != $_REQUEST["password"]) {
		return_json(40008);
		return;
	}
	return_json(0,$audience);
}
//找回密码
public function find_pwd(){
	//检测数据完整性
	switch(''){
		case $_REQUEST["conditions"]:return_json(40039);return;
	}
	//查询cid
	if(!Database($_REQUEST["cid"]))return;
	//查询str in
	if(strpos($_REQUEST["conditions"],"@") === false){
		//手机
		$tel  ='/^[0-9]{11}$/';
		preg_match($tel,$_REQUEST["conditions"],$matches);
		if($matches==array() || count($matches)==0){
			return_json(40041);return;
		}else{
			$audience_pwd=D('Audience')->find_pwd($_REQUEST["conditions"],'');
			if($audience_pwd){
				$rs=for_sms($_REQUEST["conditions"],"You password is {$audience_pwd}");
				if($rs){
					return_json(0);return;
				}else{
					return_json(40073);return;
				}
			}else{
				return_json(40001);return;	
			}
				
		}
	}else{
		//邮箱
		$email="/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/";
		preg_match($email,$_REQUEST["conditions"],$matches);
		if($matches==array() || count($matches)==0){
			return_json(40040);return;
		}else{
			$audience_pwd=D('Audience')->find_pwd('',$_REQUEST["conditions"]);
			if($audience_pwd){
				$rs=for_mail($_REQUEST["conditions"],"TeamIn Password Recovery","You password is {$audience_pwd}");
				if($rs){
					return_json(0);return;
				}else{
					return_json(40001);return;
				}
			}else{
				return_json(40001);return;	
			}
		}
	}
}
/*
 *find_person 通过姓名或者职位查找人
 *arg
 *conditions varchar
 *cid int
 ****/
public function find_person(){
	//检测数据完整性
	switch(''){
		case $_REQUEST["conditions"]:return_json(40039);return;
	}
	//查询cid
	if(!Database($_REQUEST["cid"]))return;
	$audience=D('Audience')->find_person($_REQUEST["conditions"]);
	if($audience)
		return_json(0,$audience);
	else
		return_json(40001);
}
/*
 *find_person_by_uid 通过uid查找当前个人信息
 *arg
 *uid int
 *cid int
 ****/
public function find_person_by_uid(){
	//检测数据完整性
	switch(''){
		case $_REQUEST["uid"]:return_json(40002);return;
	}
	//查询cid
	if(!Database($_REQUEST["cid"]))return;
	$audience=D('Audience')->get_audience($_REQUEST["uid"],'');
	if($audience)
		return_json(0,$audience);
	else
		return_json(40001);
}
/*
 *get_user_list 获取用户列表
 *arg
 *uid int
 *cid int
 ****/
public function get_user_list(){
	//检测数据完整性
	switch(''){
		//case $_REQUEST['timestamp']:return_json(40042);return;
		case $_REQUEST["count"]:$_REQUEST['count']=10;
		case $_REQUEST['page']:$_REQUEST['page']=1;
	}
	if(!Database($_REQUEST["cid"]))exit;
	$rs=D('Audience')->get_audience_list($_REQUEST);
	if($rs){
		return_json('0',$rs,time());
	}else{
		return_json('40001');
	}
}
/*
 *get_user_list 获取用户列表
 *arg
 *uid int
 *cid int
 ****/
public function update_audience(){
	//检测数据完整性
	switch(''){
		case $_REQUEST['uid']:return_json(40002);return;
	}
	if(!Database($_REQUEST["cid"]))exit;

	if($_FILES['audience_portrait']['name']!=''){
		$this->imgUpload();
	}
	$_REQUEST['time_update']=time();
	if(D('Audience')->update_audience($_REQUEST)){
		$rs=D('Audience')->get_audience($_REQUEST["uid"],'');
	}
	if($rs){
		return_json('0',$rs);
	}else{
		return_json(-1);
	}
}
//通过数组imUsername名单获取audience信息
/*
 *get_user_list 获取用户列表
 *arg
 *uid int
 *cid int
 ****/
public function get_audience_by_imUsernameArray(){
	switch(''){
		case $_REQUEST['imUsernameArray']:return_json(40068);return;
	}
	if(!Database($_REQUEST["cid"]))exit;
	$_REQUEST['imUsernameArray'] = explode(",",$_REQUEST['imUsernameArray']);
	//echo json_encode($_REQUEST);
	$rs=D('Audience')->get_audience_by_imUsernameArray($_REQUEST['imUsernameArray']);
	if($rs){
		return_json('0',$rs);
	}else{
		return_json(40001);
	}
}

public function imgUpload(){
	$audience_image_path = 'Public/Uploads'.'/';
	if(!is_dir($audience_image_path))mkdir($audience_image_path,0777,true);
	$audience_image_path.= 'audience'.'/';
	if(!is_dir($audience_image_path))mkdir($audience_image_path,0777,true);
	$audience_image_path.= $_REQUEST["cid"] .'/';
	if(!is_dir($audience_image_path))mkdir($audience_image_path,0777,true);

	import('ORG.Net.UploadFile');
	$config['savePath'] = $audience_image_path;
	$config['allowExts']= array('jpg', 'gif', 'png', 'jpeg');
	$config['maxSize']= 3145728;
	$config['thumb'] = true;
	$config['thumbPrefix'] = 'm_';
	$config['thumbMaxWidth'] = '480';
	$config['thumbMaxHeight'] = '320';
	$config['thumbRemoveOrigin']=true;
	$upload = new UploadFile($config);
	if(!$upload->upload()){
		$this->error($upload->getErrorMsg());
	}else{
		$uploadList = $upload->getUploadFileInfo();
		$_REQUEST['audience_portrait']	=$audience_image_path.$config['thumbPrefix'].$uploadList[0]['savename'];
	}
}
// +---------------------------------------+
// | 用户数据库同步接口  					   |
// +---------------------------------------+
// | last update author: zhanghao		   |
// | last update time  : 2014-10-28 14:50  |
// +---------------------------------------+
public function get_audience_database_synchronization(){
	if(!Database($_REQUEST["cid"]))exit;
	$_REQUEST['timestamp']=$_REQUEST['timestamp']==0?1:$_REQUEST['timestamp'];
	$rs=D('Sqlite')->get_audience_database_synchronization($_REQUEST['timestamp']);
	if($rs){
		$rs['data_type']=2;
		$rs['timestamp']=time();
		return_json('0',$rs);
	}else{
		return_json(40001);
	}
}
// +---------------------------------------+
// | 通讯录同步接口  					   |
// +---------------------------------------+
// | last update author: zhanghao		   |
// | last update time  : 2014-10-28 14:50  |
// +---------------------------------------+
	public function get_address_list_database_synchronization()
	{
		if(!Database($_REQUEST["cid"]))exit;
		$_REQUEST['timestamp']				=$_REQUEST['timestamp']==0?1:$_REQUEST['timestamp'];

		$rs['audience_list']				=D('Sqlite')->get_audience_database_synchronization($_REQUEST['timestamp']);
		$rs['department_list']				=D('Sqlite')->get_department_database_synchronization($_REQUEST['timestamp']);
		$rs['audience_department_rds_list'] =D('Sqlite')->get_audience_department_rds_database_synchronization($_REQUEST['timestamp']);
		$rs['timestamp']=time();
		return_json('0',$rs);
	}

}
?>